﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fitness_club
{
    public partial class addplan : Form

    {
        public static string constr = ConfigurationManager.AppSettings["ConStr"];
        SqlConnection con = new SqlConnection(constr);

        private int planId = -1;

        private HashSet<Control> errorControls = new HashSet<Control>();

        public addplan()
        {
            InitializeComponent();
        }

        public addplan(int planId)
        {
            InitializeComponent();
            buttonSave.Text = "Update";
            this.planId = planId;
            fetchPlanById(this.planId);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (buttonSave.Text == "Save")
            {
                try
                {
                    // objCon.Open();
                    con.Open();

                    SqlCommand objCmd = new SqlCommand();
                    objCmd.Connection = con;

                    SqlParameter param = new SqlParameter();

                    objCmd.CommandType = CommandType.StoredProcedure;
                    objCmd.CommandText = "pr_insert_subscription_plan";

                    param = new SqlParameter();
                    param.ParameterName = "@vType";
                    param.SqlDbType = SqlDbType.VarChar;
                    param.Value = textBoxPlanType.Text.ToString();
                    objCmd.Parameters.Add(param);

                    param = new SqlParameter();
                    param.ParameterName = "@vAmount";
                    param.SqlDbType = SqlDbType.Float;
                    param.Value = float.Parse(textBoxPlanAmount.Text.ToString());
                    objCmd.Parameters.Add(param);

                    param = new SqlParameter();
                    param.ParameterName = "@iDuration";
                    param.SqlDbType = SqlDbType.Int;
                    param.Value = int.Parse(textBoxDuration.Text.ToString());

                    objCmd.Parameters.Add(param);


                    objCmd.ExecuteNonQuery();
                    MessageBox.Show("Plan added succesfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("Plan adding failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    con.Close();
                }
            }
            else if (buttonSave.Text == "Update")
            {
                try
                {
                    SqlCommand updateCommand = new SqlCommand("update subscription_plan set type='" + textBoxPlanType.Text + "',amount=" + textBoxPlanAmount.Text + ",duration=" + textBoxDuration.Text + ",date_created=GETDATE() where id=" + this.planId, con);
                    con.Open();
                    updateCommand.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Plan updated succesfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("Plan updating failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
               
               
            }
            
        }

        private void addplan_Load(object sender, EventArgs e)
        {
            textBoxPlanType.Tag = "Please enter plan name";
            textBoxPlanAmount.Tag = "Please enter plan amount";
            textBoxDuration.Tag = "Please enter plan duration";

            errorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink;

            ValidateTextBox(textBoxPlanType, EventArgs.Empty);
            ValidateTextBox(textBoxPlanAmount, EventArgs.Empty);
            ValidateTextBox(textBoxDuration, EventArgs.Empty);

            
        }

        private void ValidateTextBox(object sender, EventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox.Text == "")
            {
                errorProvider1.SetError(textBox, (string)textBox.Tag);
                errorControls.Add(textBox);
            }
            else
            {
                errorProvider1.SetError(textBox, null);
                errorControls.Remove(textBox);
            }
            buttonSave.Enabled = errorControls.Count == 0;
        }

        private void fetchPlanById(int id)
        {
            try
            {
                SqlCommand sqlCommand = new SqlCommand("select * from subscription_plan where id=" + id, con);
                con.Open();
                SqlDataReader reader = sqlCommand.ExecuteReader();

                if (reader != null)
                {
                    while (reader.Read())
                    {
                        textBoxPlanType.Text = reader["type"].ToString();

                        textBoxPlanAmount.Text = reader["amount"].ToString();

                        textBoxDuration.Text = reader["duration"].ToString();

                    }
                }

            }
            finally
            {
                con.Close();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void textBoxPlanAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void textBoxDuration_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void textBoxPlanType_TextChanged(object sender, EventArgs e)
        {
            ValidateTextBox(textBoxPlanType, EventArgs.Empty);
        }

        private void textBoxPlanAmount_TextChanged(object sender, EventArgs e)
        {
            ValidateTextBox(textBoxPlanAmount, EventArgs.Empty);
        }

        private void textBoxDuration_TextChanged(object sender, EventArgs e)
        {
            ValidateTextBox(textBoxDuration, EventArgs.Empty);
        }

    }
}
